package com.renjibo.childred.fragments;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.renjibo.childred.R;
import com.renjibo.childred.adapter.MyExpandableAdapter;
import com.renjibo.childred.databases.MySqlite;
import com.renjibo.childred.databases.UserDao;
import com.renjibo.childred.inters.AllCheckBoxSet;
import com.renjibo.childred.inters.SetTextViewText;
import com.renjibo.childred.modle.GoodsBean;
import com.renjibo.childred.modle.GoodsChild;
import com.renjibo.childred.views.TopGroupView;
import com.renjibo.childred.views.XiaLa;

import java.util.ArrayList;
import java.util.List;


public class ShoppingFragment extends Fragment implements XiaLa.OnBaiduRefreshListener ,SetTextViewText
        ,AllCheckBoxSet, View.OnClickListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;
    private ExpandableListView expandableListView;
    private MyExpandableAdapter myExpandableAdapter;
    private List<GoodsBean> goodsBeanList;
    private CheckBox bottom_checkBox;
    private Button shopp_button_balance;
    private TextView textView_countPrices;
    private TextView shopp_textView_editor;
    private double allCount;
    //全部是allCheck
    private boolean allCheck;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1){
                textView_countPrices.setText("¥ " + msg.obj);
                shopp_button_balance.setText("结算("+msg.arg1+")");
            }
            if(msg.what==2){
                myExpandableAdapter.notifyDataSetChanged();
                bottom_checkBox.setChecked(allCheck);
            }
//            if(msg.what==3)
//                bottom_checkBox.setChecked(false);
        }
    };
    private UserDao userDao;

    public ShoppingFragment() {
    }


    public static ShoppingFragment newInstance(String param1, String param2) {
        ShoppingFragment fragment = new ShoppingFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        userDao = new UserDao(new MySqlite(getActivity()));
        Log.d("ShoppingFragment","onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shopping, container, false);
        setDatas();
        initView(view);
        Log.d("ShoppingFragment","onCreateView");
        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.d("ShoppingFragment","onResume");
                //设置自动展开
        //自动展开必须写在onResume方法中，否则会发生错误
        for (int i = 0; i < goodsBeanList.size(); i++) {
            expandableListView.expandGroup(i);
        }

//将ExpandableListView groupitem中系统自带的下拉箭头图标去掉
        expandableListView.setGroupIndicator(null);
    }
    public void setDatas(){
//        goodsBeanList = new ArrayList<>();
//        List<GoodsChild> good=new ArrayList<>();
//        for(int j=0;j<10;j++){
//            GoodsChild goodsChild = new GoodsChild(false,"","薯片商品"+j,getNumber(),1);
//            good.add(goodsChild);
//        }
//        List<GoodsChild> good1=new ArrayList<>();
//        for(int j=0;j<11;j++){
//            GoodsChild goodsChild = new GoodsChild(false,"","红茶商品"+j,getNumber(),1);
//            good1.add(goodsChild);
//        }
//        List<GoodsChild> good2=new ArrayList<>();
//        for(int j=0;j<8;j++){
//            GoodsChild goodsChild = new GoodsChild(false,"","飞机商品"+j,getNumber(),1);
//            good2.add(goodsChild);
//        }
//        GoodsBean goodsBean = new GoodsBean("零食店铺", good, false);
//        goodsBeanList.add(goodsBean);
//        GoodsBean goodsBean1 = new GoodsBean("茶叶店铺", good1, false);
//        goodsBeanList.add(goodsBean1);
//        GoodsBean goodsBean2 = new GoodsBean("玩具店铺", good2, false);
//        goodsBeanList.add(goodsBean2);
        goodsBeanList = userDao.chaAll();
        Log.d("ShoppingFragment","goodsBeanList:"+goodsBeanList.toString());
    }
    //随机生成一个数
    public double getNumber(){
//        Random random=new Random();
//        int i = random.nextInt(100)+1;
        return 10.01;
    }
    @Override
    public void onRefresh() {
        Toast.makeText(getActivity(),"fuehfuigi0",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("Shooping","onPause");
    }

    public void initView(View view){
        bottom_checkBox = (CheckBox)view.findViewById(R.id.bottom_checkBox);
        shopp_button_balance = (Button) view.findViewById(R.id.shopp_button_balance);
        textView_countPrices = (TextView) view.findViewById(R.id.textView_countPrices);
        shopp_textView_editor = (TextView) view.findViewById(R.id.shopp_textView_editor);
        expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListView);
        myExpandableAdapter = new MyExpandableAdapter(goodsBeanList, getActivity(), expandableListView);

        shopp_textView_editor.setOnClickListener(this);
        shopp_button_balance.setOnClickListener(this);
        //回调
        myExpandableAdapter.add(this);
        myExpandableAdapter.checkSet(this);
        expandableListView.setAdapter(myExpandableAdapter);
        bottom_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {
                new Thread(){
                    @Override
                    public void run() {
                        if(isChecked){
                            for(GoodsBean gb:goodsBeanList){
                                Log.d("zzz","bottom_checkBox");
                                gb.setGroupChex(isChecked);
                                for(GoodsChild gc:gb.getGood()){
                                    gc.setChildChe(isChecked);
                                }
                            }
                            allCheck=isChecked;
                        }else{
                            int num=0;
                            for(GoodsBean gb:goodsBeanList){
                                if(gb.isGroupChex()==false){
                                    num++;
                                    break;
                                }
                            }
                            if(num==0){
                                for(GoodsBean gb:goodsBeanList){
                                    gb.setGroupChex(false);
                                    for(GoodsChild gc:gb.getGood()){
                                        gc.setChildChe(false);
                                    }
                                }
                            }
                            allCheck=false;

                        }

                        mHandler.sendEmptyMessage(2);
                    }

                }.start();

            }
        });
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Toast.makeText(getActivity(),"setOnGroupClickListener",Toast.LENGTH_SHORT).show();

                return true;
            }
        });


    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            Toast.makeText(getActivity(),"setOnGroupClickListener",Toast.LENGTH_SHORT).show();

            return true;
        }
    });
    }
    //修改值
    @Override
    public void setTextView() {
        new Thread(){
            @Override
            public void run() {
                double allCount1=0;
                int countNum=0;
                for(GoodsBean gb:goodsBeanList){
                    for(GoodsChild gc:gb.getGood()){
                        //Log.d("zzz","gc:"+gc.isChildChe());
                        if(gc.isChildChe()) {
                            allCount1 += gc.getNum() * gc.getPrice();
                            countNum++;
                            //Log.d("zzz", "count:" + allCount1);
                        }else{
                            mHandler.sendEmptyMessage(3);
                        }
                    }
                }
                Message mess=Message.obtain();
                mess.what=1;
                mess.arg1=countNum;
                float countAll= (float) allCount1;
                mess.obj=countAll;
                mHandler.sendMessage(mess);
            }
        }.start();
        //textView_countPrices.setText("¥ " + allCount1);
    }
    //监听子条目的checkbox 改变
    @Override
    public void setCheckBox() {
        int num=0;
        for(GoodsBean gb:goodsBeanList){
            if(gb.isGroupChex()){
                num++;
            }
        }
        if(num==goodsBeanList.size()){
            bottom_checkBox.setChecked(true);
        }else{
            bottom_checkBox.setChecked(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //主编辑
            case R.id.shopp_textView_editor:
                for(GoodsBean gb:goodsBeanList){
                    gb.setGoneView(!gb.isGoneView());
                }
                myExpandableAdapter.notifyDataSetChanged();
                break;
            //结算
            case R.id.shopp_button_balance:

                break;
        }
    }
}
